/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar.swing;

import java.awt.Color;

import javax.swing.JSpinner;
import javax.swing.JTextField;

import com.toedter.components.JSpinField;

/**
 * A JSpinner specialized to format the time with 2 digits
 * 
 * @version 1.0.1 - 2009-06-01 - Added Time controls
 * @author servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class TextJSpinField extends JSpinField {

	private static final long serialVersionUID = 1L;
	
	private boolean useTwoDigits = true;

	/**
	 * 
	 */
	public TextJSpinField() {
		super();
	}
	
	/**
	 * @param min
	 * @param max
	 */
	public TextJSpinField(int min, int max) {
		super(min, max);
	}
	

	/* (non-Javadoc)
	 * @see com.toedter.components.JSpinField#setValue(int)
	 */
	@Override
	protected void setValue(int newValue, boolean updateTextField, boolean firePropertyChange) {
		if (!useTwoDigits || newValue > 9) {
			super.setValue(newValue, updateTextField, firePropertyChange);
		} else {
			int oldValue = value;
			if (newValue < min) {
				value = min;
			} else if (newValue > max) {
				value = max;
			} else {
				value = newValue;
			}

			if (updateTextField) {
				textField.setText("0"+Integer.toString(value));
				textField.setForeground(Color.black);
			}

			if (firePropertyChange) {
				firePropertyChange("value", oldValue, value);
			}
		}
	}

	/**
	 * @return textField
	 */
	public JTextField getTextField() {
		return textField;
	}

	/**
	 * @return the useTwoDigits
	 */
	public boolean isUseTwoDigits() {
		return this.useTwoDigits;
	}

	/**
	 * @param useTwoDigits the useTwoDigits to set
	 */
	public void setUseTwoDigits(boolean useTwoDigits) {
		this.useTwoDigits = useTwoDigits;
	}

	/* (non-Javadoc)
	 * @see com.toedter.components.JSpinField#setMaximum(int)
	 */
	@Override
	public void setMaximum(int arg0) {
		super.setMaximum(arg0);
		JSpinner spinner = (JSpinner)getSpinner();
		((WrapSpinnerNumberModel)spinner.getModel()).setMaximum(arg0);
	}

	/* (non-Javadoc)
	 * @see com.toedter.components.JSpinField#setMinimum(int)
	 */
	@Override
	public void setMinimum(int arg0) {
		super.setMinimum(arg0);
		JSpinner spinner = (JSpinner)getSpinner();
		((WrapSpinnerNumberModel)spinner.getModel()).setMinimum(arg0);
		
	}
	
}
